﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Net.Http;
using System.IO;
using StudentWeb.DAL;
using StudentWeb.Model;
using StudentWeb.Common;

namespace StudentWeb.BLL
{
    public class ProductMo
    {
        #region 移动端产品信息 分页列表
        public IResult GetProductMoList(int pageIndex, int pageSize, string wc = null)
        {
            var list = new ProductDataDAO().GetList(pageIndex, pageSize, wc);
            return new DataResult<PagedList<ProductData>>(list);
        }
        #endregion

        #region 移动端产品信息 单条记录
        public IResult GetProductMoInfo(int productID)
        {
            var res = new ProductDataDAO().GetInfo(productID);
            return new DataResult<ProductData>(res);
        }
        #endregion

        #region 移动端产品信息 保存
        public bool SaveProductMoInfo(dynamic data, int userid)
        {
            int productID = data.ProductID ?? 0;
            var result = 0;
            if (productID > 0)
            {
                var record = new ProductDataDAO().GetInfo(productID);
                record.ProductNo = data.ProductNo;
                record.ProductName = data.ProductName;
                record.ProductPrice = data.ProductPrice;
                record.ProductKind = data.ProductKind;
                record.Stock = data.Stock;
                record.ProductImage = data.ProductImage ?? "";
                record.ProductPublishdate = data.ProductPublishdate;
                record.EditDateTime = DateTime.Now;
                record.EditUserID = userid;
                result = new ProductDataDAO().Update(record);
            }
            else if (productID == 0)
            {
                var product = new ProductData()
                {
                    ProductNo = data.ProductNo,
                    ProductState = 1,
                    ProductName = data.ProductName,
                    ProductPrice = data.ProductPrice,
                    Stock = data.Stock,
                    ProductKind = data.ProductKind,
                    ProductPublishdate = data.ProductPublishdate,
                    ProductImage = data.ProductImage ?? "",
                    RegUserID = userid,
                    EditDateTime = DateTime.Now,
                    EditUserID = userid,
                };
                result = new ProductDataDAO().Insert(product);
            }
            return result > 0;
        }
        #endregion

        #region 移动端产品信息 图片
        public DataResult UploadFile(HttpFileCollection files)
        {
            var res = new DataResult(null, ResultCode.Fail);
            try
            {
                var file = files["file"];
                if (file != null)
                {
                    byte[] buffer;
                    if (file.ContentLength > 1048576 * 10) //10MB
                    {
                        return new DataResult(null, ResultCode.Fail, "文件超出大小限制");
                    }
                    var fileFolder = "/Images/"; //存放图片的文件夹
                    var filePath = HttpContext.Current.Server.MapPath(fileFolder); //存放图片的完整文件夹
                    if (!Directory.Exists(filePath)) //判断文件夹是否存在，如不存在请新创建
                    {
                        Directory.CreateDirectory(filePath);
                    }
                    string fileExt = Path.GetExtension(file.FileName); //取文件扩展名
                    var fileName = Guid.NewGuid().ToString().Replace("-", "") + fileExt; //生成随机文件名
                    var fullPath = Path.Combine(filePath, fileName); //完整路径（文件夹+文件名）
                    file.SaveAs(fullPath); //保存
                    var url = fileFolder + fileName; //图片URL地址
                    res = new DataResult(new { url });
                }
                else
                {
                    res = new DataResult(null, ResultCode.Fail);
                }
            }
            catch (Exception ex)
            {
                res = new DataResult(null, ResultCode.Fail, ex.Message);
            }
            return res;
        }
        #endregion

        #region  移动端设置产品状态
        public IResult SetProductMoState(int productID, int productState)
        {
            var record = new ProductDataDAO().GetInfo(productID);
            if (record == null)
            {
                return new Result(ResultCode.Fail, "为找到此ID的产品");

            }
            int newState = 0;
            if (productState == 1)
            {
                newState = 2;
            }
            else if (productState == 2)
            {
                newState = 1;
            }

            record.ProductState = newState;
            var result = new ProductDataDAO().Update(record);
            return result >= 1 ? new Result() : new Result(ResultCode.Fail, "修改失败");
        }
        #endregion

        #region 移动端产品信息全部记录
        public IResult GetProductMoList()
        {
            var list = new ProductDataDAO().GetList();
            return new DataResult(list);
        }
        #endregion
    }
}
